clear all
cd "data"

use canvasser_endline_baseline_clean_ismp2_phone.dta, replace
gen universe = "imm_phone"

append using canvasser_endline_baseline_clean_ismp2_canvass.dta
replace universe = "imm_canvass" if missing(universe)

append using partisan_canvasser_endline_baseline_clean.dta
replace universe = "partisan_canvass" if missing(universe)

egen canvasser_id2 = group(canvasser_id universe)
drop canvasser_id
rename canvasser_id2 canvasser_id

keep if t1_respondent == 1

// Are there HTEs by prior exposure to perspective-getting / deep canvassing?
// Coding is slightly different across studies, create a consistent variable
replace t0_prior_deepcanvasser = t0_canvassed_phase_one if missing(t0_prior_deepcanvasser)
tab t0_prior_deepcanvasser universe, col

tabstat days_between t0_prior_campaign_experience t0_prior_deepcanvasser, by(universe) s(p50 mean)

// Rename some variables from the partisan canvass to match immigration
drop *_diff
foreach i in 0 1 {
	replace t`i'_therm_trump_oppose = t`i'_therm_not_trump_voters if missing(t`i'_therm_trump_oppose)
	replace t`i'_therm_gop_voters = t`i'_therm_rep_voters if missing(t`i'_therm_gop_voters)
	drop t`i'_therm_not_trump_voters t`i'_therm_rep_voters 
	gen t`i'_therm_partisan_voters_diff = t`i'_therm_dem_voters - t`i'_therm_gop_voters
	gen t`i'_therm_trump_voters_diff = t`i'_therm_trump_oppose - t`i'_therm_trump_voters
}


keep canvasser_id t*_therm_trump_voters t*_trump_voters_diff t*_therm_gop_voters t*_partisan_voters_diff ///
	t*_therm_dem_voters t*_therm_trump_oppose universe days_between t0_prior_deepcanvasser number_convos

rename t1_* *1
rename t0_* *0

foreach var in therm_trump_voters therm_gop_voters {
	gen `var'_change = `var'1 - `var'0
}

save combined.dta, replace

// Paired t-test
ttest therm_trump_voters1 = therm_trump_voters0
ttest therm_gop_voters1 = therm_gop_voters0

	
reshape long therm_trump_voters therm_trump_oppose therm_trump_voters_diff therm_gop_voters ///
	therm_dem_voters therm_partisan_voters_diff, i(canvasser_id) j(post) // id = unique identifier for each canvasser
	
bysort prior_deepcanvasser0: areg therm_trump_voters i.post, a(canvasser_id)
bysort prior_deepcanvasser0: areg therm_gop_voters i.post, a(canvasser_id)

drop if missing(number_convos)
bysort universe: sum number_convos, det
gen number_convos_above_median = 0
	replace number_convos_above_median = 1 if universe == "imm_phone" & number_convos > 13
	replace number_convos_above_median = 1 if universe == "imm_canvass" & number_convos > 6
tabstat number_convos, by(number_convos_above_median)
bysort number_convos_above_median: areg therm_trump_voters i.post, a(canvasser_id)
bysort number_convos_above_median: areg therm_gop_voters i.post, a(canvasser_id)
